package com.jd.pet.service.impl;


import com.jd.basic.config.BusinessException;
import com.jd.basic.service.IBaiduAiAuditService;
import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.LoginContext;
import com.jd.org.domain.Employee;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.pet.domain.Pet;
import com.jd.pet.domain.PetDetail;
import com.jd.pet.domain.PetOnlineAuditLog;
import com.jd.pet.mapper.PetDetailMapper;
import com.jd.pet.mapper.PetMapper;
import com.jd.pet.mapper.PetOnlineAuditLogMapper;
import com.jd.pet.service.IPetService;
import com.jd.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import sun.util.calendar.LocalGregorianCalendar;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;



    @Transactional
    @Override
    public void save(Pet pet) {
        //保存宠物信息
        super.save(pet);
        //保存宠物明细
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    @Override
    public void update(Pet pet) {
        //修改宠物信息
        super.update(pet);
        //修改宠物明细
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }
    }

    @Transactional
    @Override
    public void remove(Long id) {
        //删除宠物明细
        petDetailMapper.delPetDetailByPetId(id);
        //删除宠物信息
        super.remove(id);

    }

    @Override
    public void patchDelete(Long[] ids) {
        //删除宠物明细
        petDetailMapper.patchDeleteByPetId(ids);
        //删除宠物信息
        super.patchDelete(ids);
    }

    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        //获取审核人
        Logininfo logininfo = LoginContext.getLoginUser(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        //上架不能用批量操作
        for (Long id : ids) {
            //上架自动审核文本
            Pet pet = petMapper.loadById(id);
            String auditText = pet.getName();
            Boolean textBoo = baiduAiAuditService.textAudit(auditText);
            //审核图片
            String petResources = pet.getResources();

            Boolean imageBoo = true;
            if (!StringUtils.isEmpty(petResources)){
                Boolean imageAudit = baiduAiAuditService.imageAudit(petResources);
            }
            //审核通过
            if (textBoo && imageBoo){
                //数据库操作
                Date onsaletime = new Date();
                petMapper.onsale(onsaletime,ids);
                //审核成功note
                //记录审核日志
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                //state = 1,审核通过
                auditLog.setState(1);
                auditLog.setPetId(id);
                System.out.println(auditLog.getPetId());
                //记录审核人Id
                auditLog.setAuditId(employee.getId());
                System.out.println(auditLog.getAuditId());
                auditLog.setNote("审核通过");
                System.out.println(auditLog.getNote());
                petOnlineAuditLogMapper.save(auditLog);

            } else {
                //审核失败 note - 则当前为下架状态 - 如果审核失败
                //记录日志
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setState(0);
                auditLog.setPetId(id);
                //记录审核人Id
                auditLog.setAuditId(employee.getId());
                auditLog.setNote("审核失败,宠物名称或图片不合法");
                petOnlineAuditLogMapper.save(auditLog);
                throw new BusinessException("上架失败");
            }
        }

    }

    @Override
    public void offsale(List<Long> ids, HttpServletRequest request) {
        //下架：修改state为0，offsaletime为当前时间 ,可以用批量操作
        Date offsaletime = new Date();
        //批量操作
        petMapper.offsale(offsaletime,ids);
    }
}
